﻿using Data.Initializers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data.Repositories
{
    public class HoaDonRepository
    {
        private SqlConnection _connect;
        private SqlDataAdapter _sqlAdapter;
        public string CapNhatHoaDon(HoaDon hd)
        {
            try
            {
                if (hd.Id != 0)
                {
                    ConnectDB.CloseConnection(_connect);
                    _connect = new SqlConnection();
                    _connect = ConnectDB.GetSQlConnection(_connect);
                    var cm = _connect.CreateCommand();
                    cm.CommandText = "dbo.C_CAPNHAT_TT_HOADON";
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@id", SqlDbType.BigInt).Value = hd.Id;
                    cm.Parameters.Add("@id_trangthai", SqlDbType.Int).Value = hd.TrangThai;
                    cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                    //cm.Parameters.Add(newReqNumber);
                    cm.ExecuteNonQuery();
                    string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                    if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                }
                else
                {
                    //Them moi
                    ConnectDB.CloseConnection(_connect);
                    _connect = new SqlConnection();
                    _connect = ConnectDB.GetSQlConnection(_connect);
                    var cm = _connect.CreateCommand();
                    cm.CommandText = "dbo.C_INSERT_HOADON";
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@nguoi_dung_id", SqlDbType.BigInt).Value = hd.NguoiDungId;
                    cm.Parameters.Add("@tong_tien", SqlDbType.BigInt).Value = hd.TongTien;
                    cm.Parameters.Add("@trang_thai", SqlDbType.Int).Value = 0;
                    cm.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    cm.ExecuteNonQuery();
                    string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                    if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                    foreach (var ct in hd.lstChiTiet)
                    {
                        if (ct.Id == 0)
                        {
                            ConnectDB.CloseConnection(_connect);
                            _connect = new SqlConnection();
                            _connect = ConnectDB.GetSQlConnection(_connect);
                            var cm1 = _connect.CreateCommand();
                            cm1.CommandText = "dbo.C_INSERT_HOADONCHITIET";
                            cm1.CommandType = CommandType.StoredProcedure;
                            cm1.Parameters.Add("@hoa_don_id", SqlDbType.BigInt).Value = Convert.ToInt64(NewQuoteNumber);
                            cm1.Parameters.Add("@san_pham_id", SqlDbType.BigInt).Value = ct.SanPhamId;
                            cm1.Parameters.Add("@san_pham_gia", SqlDbType.BigInt).Value = ct.GiaTien;
                            cm1.Parameters.Add("@so_luong", SqlDbType.BigInt).Value = ct.SoLuong;
                            cm1.Parameters.Add("@trang_thai", SqlDbType.Int).Value = ct.TrangThai;
                            cm1.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                            cm1.ExecuteNonQuery();
                            string NewQuoteNumber1 = cm1.Parameters["@NewQuoteNumber"].Value.ToString();
                            if (NewQuoteNumber1 == "0" || NewQuoteNumber1 == "") throw new Exception();
                        }

                    }

                }
                if (hd.lstChiTiet != null)
                {
                    foreach (var chitiet in hd.lstChiTiet)
                    {
                        ConnectDB.CloseConnection(_connect);
                        _connect = new SqlConnection();
                        _connect = ConnectDB.GetSQlConnection(_connect);
                        var cm1 = _connect.CreateCommand();
                        cm1.CommandText = "dbo.C_UPDATE_MUASANPHAM";
                        cm1.CommandType = CommandType.StoredProcedure;
                        cm1.Parameters.Add("@id", SqlDbType.BigInt).Value = chitiet.SanPhamId;
                        cm1.Parameters.Add("@soluong", SqlDbType.BigInt).Value = chitiet.SoLuong;
                        cm1.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                        cm1.ExecuteNonQuery();
                        string NewQuoteNumber1 = cm1.Parameters["@NewQuoteNumber"].Value.ToString();
                        if (NewQuoteNumber1 == "0" || NewQuoteNumber1 == "") throw new Exception();
                    }
                    
                }
                return "Thành công";
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDB.CloseConnection(_connect);
            }
        }

        public DataTable DanhSachHoaDon(long UserId)
        {
            try
            {
                ConnectDB.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDB.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_DANHSACH_HOADON";
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@userid", SqlDbType.BigInt).Value = UserId;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDB.CloseConnection(_connect);
            }
        }
        public DataTable ChiTietHoaDon(long HoaDonId)
        {
            
            try
            {
                ConnectDB.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDB.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_DANHSACH_CHITIETHOADON";
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@hoadonid", SqlDbType.BigInt).Value = HoaDonId;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDB.CloseConnection(_connect);
            }
        }
    }
}
